import 'package:duo_zui/page/Login_page.dart';
import 'package:duo_zui/page/SettingPage.dart';
import 'package:duo_zui/page/TabbedAppBarMain.dart';
import 'package:duo_zui/page/WriteCommentPage.dart';
import 'package:duo_zui/page/comment_page.dart';
import 'package:duo_zui/page/web_view_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:duo_zui/page/search_page.dart';
import 'package:duo_zui/page/person_center_page.dart';
import 'package:duo_zui/page/MyHomePage.dart';

///https://www.jianshu.com/p/b9d6ec92926f

class MyRouter {
  static const homePage = 'app://';
  static const detailPage = 'app://DetailPage';
  static const playListPage = 'app://VideosPlayPage';
  static const searchPage = 'app://SearchPage';
  static const photoHero = 'app://PhotoHero';
  static const personDetailPage = 'app://PersonDetailPage';
  static const userPage = 'app://userPage';
  static const articelPage = 'app://articelPage';
  static const loginPage = 'app://loginPage';
  static const myhomePage = 'app://myhomePage';
  static const settingPage = 'app://settingPage';
  static const writeCommentPage = 'app://writeCommentPage';
  static const commentList = 'app://commentList';

  Widget _getPage(String url, dynamic params) {
    if (url.startsWith('https://') || url.startsWith('http://')) {
      //return WebViewPage(url, params: params);
    } else {
      switch (url) {
        case homePage:
          return TabbedAppBarMain();
        case searchPage:
          return SearchPage(searchHintContent: params);
        case userPage:
          return PersonCenterPage();
        case articelPage:
          //return CuriosityWebView(url: params['url'], article: params['article']);
          return DetailsPage(url: params['url'], article: params['article']);
        case loginPage:
          return LoginPage(url: params['url']);
        case personDetailPage:
          return PersonCenterPage(islogin: params['islogin'], myUser: params['myUser'],);
        case myhomePage:
          return MyHomePage(user: params['user'],);
        case settingPage:
          return SettingPage();
        case writeCommentPage:
          return WriteCommentPage(articleId: params['articleId'], post: params['article']);
        case commentList:
          return CommentPage(themeId: params['articleId'], post: params['article']);
      }
    }
    return null;
  }

  MyRouter.pushNoParams(BuildContext context, String url) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return _getPage(url, null);
    }));
    //Navigator.push(context, SlideRightRoute(page: _getPage(url, null)));
  }

  MyRouter.push(BuildContext context, String url, dynamic params) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return _getPage(url, params);
    }));
    //Navigator.push(context, SlideRightRoute(page: _getPage(url, params)));
  }
}

class SlideRightRoute extends PageRouteBuilder {
  final Widget page;
  SlideRightRoute({this.page})
      : super(
    pageBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        ) =>
    page,
    transitionsBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        Widget child,
        ) =>
        SlideTransition(
          position: Tween<Offset>(
            begin: const Offset(1, 0),
            end: Offset.zero,
          ).animate(animation),
          child: child,
        ),
  );
}
